//
//  WSSureOrderDbV.m
//  FD
//
//  Created by xiaofang wang on 2018/11/5.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "WSSureOrderDbV.h"
@interface WSSureOrderDbV ()
/**1. 外送view */
@property (nonatomic, strong) UIView *wsDiv;
/**2.自提view 默认隐藏*/
@property (nonatomic, strong) UIView *ztDiv;

/**自提门店lb*/
@property (nonatomic, strong) UILabel *zitiMenDianLb;
/**2，地址 可以重新选择地址 添加手势*/
@property (nonatomic, strong) UIView *addressView;
/**2-1，地址 可以重新选择地址 添加手势*/
@property (nonatomic, strong) UIView *addressViewZT;
/**2--2点击手势无响应，父试图比子视图小，怎么点都没响应 透明的按钮 */
@property (nonatomic, strong) UIButton *specialBtn;


/**3.送餐时间*/
@property (nonatomic, strong) UIView *sendTimeView;

@end

@implementation WSSureOrderDbV

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];

        [self setupZTView];
        [self setupWSView];
        
        [self hideZTView]; //默认隐藏
    }
    return self;
}

//外送对应的
- (void)setupWSView {
    self.wsDiv.frame = self.bounds;
    [self addSubview:self.wsDiv];
    
    self.addressView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 50);
    self.sendTimeView.frame = CGRectMake(0, self.addressView.rb_bottom+1, SCREEN_WIDTH, 40);//留出1px的间隙，背景色是灰色的，其他的背景都是白色的。

    //来醉了
    CGFloat sw = self.bounds.size.width;
    CGFloat w = sw-40-10;
    self.addressLb1.frame = CGRectMake(40, 0, w, 50);
    [self.addressView addSubview:self.addressLb1];
    
    
    [self.wsDiv addSubview:self.addressView];
    [self.wsDiv addSubview:self.sendTimeView];
}

//自提对应的
- (void)setupZTView {
    self.ztDiv.frame = self.bounds;
    [self addSubview:self.ztDiv];
    
    self.zitiMenDianLb.frame = CGRectMake(15, 0, 200, 40);
    
    //底部添加一个白色的view
    UIView *whiteBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    whiteBgView.backgroundColor = [UIColor whiteColor];
    [whiteBgView addSubview:self.zitiMenDianLb];
    [self.ztDiv addSubview:whiteBgView];

    self.addressViewZT.frame = CGRectMake(0, self.zitiMenDianLb.rb_bottom+1, SCREEN_WIDTH, 50);
    CGFloat sw = self.bounds.size.width;
    CGFloat w = sw-40-10;
    self.addressLb2.frame = CGRectMake(40, 0, w, 50);
    [self.addressViewZT addSubview:self.addressLb2];

    //添加按钮，响应点击时间
    self.specialBtn.frame = self.addressViewZT.bounds;
    [self.addressViewZT addSubview:self.specialBtn];
    
    [self.ztDiv addSubview:self.addressViewZT];
}


- (UIView *)wsDiv {
    if (!_wsDiv) {
        _wsDiv = [UIView new];
        _wsDiv.backgroundColor = kMainLightGrayColor; //不给颜色默认是透明的
    }
    return _wsDiv;
}
- (UIView *)ztDiv {
    if (!_ztDiv) {
        _ztDiv = [UIView new];
        _ztDiv.backgroundColor = kMainLightGrayColor;
    }
    return _ztDiv;
}

//----------
- (UILabel *)zitiMenDianLb {
    if (!_zitiMenDianLb) {
        //创建一个大的白色的背景
        _zitiMenDianLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
        _zitiMenDianLb.text = @"自提门店";
        _zitiMenDianLb.backgroundColor = [UIColor whiteColor];
    }
    return _zitiMenDianLb;
}

- (UIView *)addressView {
    if (!_addressView) {
        _addressView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        UIImageView *iconIv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"address"]];
        //
        _addressView.backgroundColor = [UIColor whiteColor]; //white
        iconIv.frame = CGRectMake(15, (50-20)*0.5, 15, 20);
        [_addressView addSubview:iconIv];
        
        //地址
        CGFloat w = (SCREEN_WIDTH - iconIv.rb_right - 10);
        self.addressLb1.frame = CGRectMake(iconIv.rb_right + 5, 0, w, 50);
    
        [_addressView addSubview:self.addressLb1];
        
        //添加点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAddressGes:)];
        [_addressView addGestureRecognizer:tap];
    }
    return _addressView;
}


//都是懒加载的时候u，懒加载里面在写懒加载就会有问题，。布局又问你他
//到底是先创建谁呢？？？？？？？？18-11-7
- (UIView *)addressViewZT {
    if (!_addressViewZT) {
        _addressViewZT =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        _addressViewZT.backgroundColor = [UIColor whiteColor]; //不给颜色默认是透明的
        UIImageView *iconIv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"address"]];
        //
        iconIv.frame = CGRectMake(15, (50-20)*0.5, 15, 20);
        [_addressViewZT addSubview:iconIv];
        
        //地址 这样写有问题，
//        CGFloat w = SCREEN_WIDTH - iconIv.rb_right - 10;
//        self.addressLb1.frame = CGRectMake(iconIv.rb_right + 5, 0, w, 50);
//        [_addressViewZT addSubview:self.addressLb1];
    }
    return _addressViewZT;
}

- (UIView *)sendTimeView {
    if (!_sendTimeView) {
        _sendTimeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 45)];
        _sendTimeView.backgroundColor = [UIColor whiteColor];
        //(15, y,15,15);
        UIImageView *iconIv = [[UIImageView alloc] initWithFrame:CGRectMake(15, (45-15)*0.5, 15, 15)];
        iconIv.image = [UIImage imageNamed:@"time"];
        [_sendTimeView addSubview:iconIv];
        
        UILabel *timeLb = [UILabel new];
        timeLb.text = @"预计送达时间40分钟";
        timeLb.frame = CGRectMake(iconIv.rb_right + 5, (45-20)*0.5, 200, 20);
        timeLb.backgroundColor = [UIColor whiteColor];
        [_sendTimeView addSubview:timeLb];
    }
    return _sendTimeView;
}

//!!!!:特殊
- (UILabel *)addressLb1 {
    if (!_addressLb1) {
        _addressLb1 = [UILabel new];
        _addressLb1.text = NSLocalizedString(Order_chooseAddress, @"请选择送餐地址") ;
        _addressLb1.numberOfLines = 0;
    }
    return _addressLb1;
}

- (UILabel *)addressLb2 {
    if (!_addressLb2) {
        _addressLb2 = [UILabel new];
        _addressLb2.text = @"请选择送餐地址";
        _addressLb2.numberOfLines = 0;
    }
    return _addressLb2;
}

#pragma mark - 接口
- (void)hideWSView {
//    这种方式是有大bug等 不能共用一份，
// 必须各用个的：addressViewZT addressView
    self.wsDiv.hidden = YES;
    self.ztDiv.hidden = NO;
    
//    [self.wsDiv removeFromSuperview];
//    [self addSubview:self.ztDiv];
}

- (void)hideZTView {
    self.ztDiv.hidden = YES;
    self.wsDiv.hidden = NO;
}

#pragma mark - 点击事件
- (void)tapAddressGes:(UITapGestureRecognizer *)tap {
    FDLog(@"点击了");
    if (self.wsSureOrderDbVBlock) {
        self.wsSureOrderDbVBlock();
    }
}

#pragma mark - 特殊的按钮解决手势无响应问题
- (UIButton *)specialBtn {
    if (!_specialBtn) {
        _specialBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_specialBtn addTarget:self action:@selector(clickBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _specialBtn;
}
//点击了
- (void)clickBtnAction:(UIButton *)btn {
    FDLog(@"1111111");
    if (self.wsSureOrderDbVBlock) {
        self.wsSureOrderDbVBlock();
    }
}
@end
